<?php
/********************************************************************
//                         Koi downloader
//                     =======================
//                   Copyright (C) 2007 Steve H
//                      http://ComicCMS.com/
//-------------------------------------------------------------------
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 3
//  of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  http://www.gnu.org/copyleft/gpl.html
//
********************************************************************/

header('cache-control: no-cache');
header('pragma: no-cache');

$GLOBALS['project'] = array(
    'name'=>'ComicCMS',
    'author'=>'Steve H',
    'url'=>'http://comiccms.com/',
    'version'=>'0.2',
    'blurb'=>'A php program that can set up and manage an entire webcomic site without the need for mysql',
    'continue'=>'admin/', // The url to direct the user to after Koi has finished
);

// setting => value
// Koi settings
$GLOBALS['settings'] = array(
    'gz'=>true, // Allow gz compressed transfer? (Need gz files on all mirrors)
    'keeplock'=>true, // Keep lock.php file for IP lock in your installation script
);


// url => weight
// If Koi is left to decide which mirror to use the weight will
// dictate the likelyhood of each mirror being used. It's relative
// to the list so 10:100 is the same as 1:10
$GLOBALS['mirrors'] = array(
    'http://comiccms.com/repo/'=>1,
);


// ext => content-type
// File extensions to send headers for
$GLOBALS['filetypes'] = array(
    'png'=>'image/png',
    'gif'=>'image/gif',
    'jpg'=>'image/jpeg',
    'jpeg'=>'image/jpeg',
    'js'=>'text/javascript',
    'css'=>'text/css',
    'txt'=>'text/plain',
    'html'=>'text/html',
    'xml'=>'text/xml',
);



/*
 * =============================================
 *         Do not edit below this point
 * =============================================
 * 
 *  Any further modifications should be done in
 *  design.php and whatever other files you use
 * 
 */



//////////////////////////////
// Other files
if (isset($_GET['f'])) {
    // Headers
    $ext = strtolower(substr($_GET['f'],(strrpos($_GET['f'],'.')+1)));
    if (isset($GLOBALS['filetypes'][$ext])) header('content-type: '.$GLOBALS['filetypes'][$ext]);
    // Print file
    echo koi_file_unpack($_GET['f']);
    exit;
}



//////////////////////////////
// AJAJ calls
if (isset($_GET['stage'])) koi_import('koi_apply.php');



//////////////////////////////
// HTML call
$status = 'good';
$write = false;
if (is_file('lock.php')) {
    $lock = file('./lock.php');
    if ( (rtrim($lock[0])=='<?php exit; ?' . '>') && ($_SERVER['REMOTE_ADDR']!=$lock[1]) ) {
        $status = 'lock';
        $write = true;
    } elseif (is_writeable('./')) {
        $write = true;
    }
} elseif ($fhandle = @fopen('./lock.php','w')) {
    if (@fwrite($fhandle,'<?php exit; ?' . '>' . "\n" . $_SERVER['REMOTE_ADDR'])) {
        $write = true;
    }
    fclose($fhandle);
}
if (!$write) $status = 'write';

koi_import('design.php');



/*
 * ==============================================
 *            Now the file handlers
 * ==============================================
 * 
 */



function koi_import($file) {
    global $status; // Pass the variable status across for design.php
    eval('?>'.koi_file_unpack($file));
}


function koi_file_unpack($file) {
    $filelist = koi_filelist();
    $func = 'koifile_'.str_replace('.','_',$file);
    if ( (in_array($file,$filelist)) && (function_exists($func)) ) {
        return base64_decode($func());
    } else return false;
}



/*
 * ==============================================
 *        Every single file (in base64)
 * ==============================================
 * 
 */

